package com.vf.cloud.paas.app.master.api.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.vf.cloud.paas.app.master.api.file.service.IThumbnail;
import com.vf.cloud.paas.app.master.common.domain.Project;
import com.vf.cloud.paas.app.master.common.domain.Scene;
import com.vf.cloud.paas.app.master.common.domain.User;
import com.vf.cloud.paas.app.master.common.repository.base.Thumbnail;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.util.DateUtil;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import com.vf.cloud.paas.common.util.WebUtils;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/project")
public class ProjectController {

	@Autowired
	private IThumbnail thumbnailImpl;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<List<Project>> list(HttpServletRequest request, @RequestBody Project param) {

		User user = WebUtils.getCurUser(request);
		if (user == null) {
			return R.expired();
		}

		if (StrKit.equals("admin", user.getRoleMark())) {
			String sqlExceptSelect = " from " + Project.TABLE_NAME + "  a WHERE a.classify_id='" + param.getClassifyId()
					+ "' ";
			if (!StrKit.isBlank(param.getName())) {
				sqlExceptSelect += " and name like '%" + param.getName() + "%' ";
			}
			sqlExceptSelect += "  ORDER BY   a.modify_time desc ";
			return R.ok(Project.dao.find("select *  " + sqlExceptSelect));
		} else {
			String sqlExceptSelect = " from " + Project.TABLE_NAME + "  a WHERE a.id in (select project_id from master_user_project where master_id='"+user.getId()+"' ) and a.classify_id='" + param.getClassifyId()+"'";
			if (!StrKit.isBlank(param.getName())) {
				sqlExceptSelect += " and name like '%" + param.getName() + "%' ";
			}
			sqlExceptSelect += "  ORDER BY   a.modify_time desc ";
			return R.ok(Project.dao.find("select *  " + sqlExceptSelect));
		}
	}

	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<Project> save(HttpServletRequest request, MultipartFile thumbnailFile, Project asset) {
		try {
			User user = WebUtils.getCurUser(request);
			Thumbnail thumbnailEntity = null;
			if (thumbnailFile != null) {
				thumbnailEntity = new Thumbnail();
				thumbnailEntity.setSize(thumbnailFile.getSize());
				thumbnailEntity.setName(asset.getName());
				thumbnailEntity.setContent(new Binary(thumbnailFile.getBytes()));
			}

			if (StrKit.notBlank(asset.getId())) {
				asset.setModifyUserId(user.getCreateUserId());
				asset.setModifyTime(DateUtil.getLocalDateTime());
				if (asset.update()) {
					if (thumbnailEntity != null) {
						try {
							thumbnailEntity.setId(asset.getId());
							thumbnailImpl.save(thumbnailEntity);
						} catch (Exception e) {
						}
					}

					return R.ok(asset);
				} else {
					return R.failed();
				}
			} else {
				asset.setId(UuidUtil.getUUID());
				asset.setCreateUserId(user.getId());
				asset.setCreateTime(DateUtil.getLocalDateTime());
				if(StrKit.isBlank(asset.getClassifyId())) {
					asset.setClassifyId("-1");
				}
				if (asset.save()) {
					if (thumbnailEntity != null) {
						try {
							thumbnailEntity.setId(asset.getId());
							thumbnailImpl.save(thumbnailEntity);
						} catch (Exception e) {
						}
					}
					return R.ok(asset);
				} else {
					return R.failed();
				}
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<String> delete(@RequestBody Project param) {
		if (param == null) {
			return R.failed("参数必填.");
		}
		if (Project.dao.deleteById(param.getId())) {
			thumbnailImpl.deleteById(param.getId());
			Db.delete("delete from master_user_project where project_id=?", param.getId());
			Db.delete("delete from api_test_custom where project_id=?", param.getId());

			return R.ok();
		}
		return R.failed();
	}

	@RequestMapping(value = "/scene", method = RequestMethod.POST)
	public R<String> scene(HttpServletRequest request, @RequestBody Project param) {
		Scene scene = new Scene();
		scene.setId(UuidUtil.getUUID());
		scene.setName(param.getName());
		scene.setClassifyId("-1");
		scene.setProjectId(param.getId());
		Thumbnail thumbnail = thumbnailImpl.findById(param.getId());
		User user = WebUtils.getCurUser(request);
		scene.setId(UuidUtil.getUUID());
		scene.setCreateUserId(user.getId());
		scene.setCreateTime(DateUtil.getLocalDateTime());

		if (scene.save()) {
			if (thumbnail != null) {
				thumbnail.setId(scene.getId());
				thumbnailImpl.save(thumbnail);
			}
			return R.ok();
		}
		return R.failed();
	}

}
